<?php
session_start();
if (isset($_SESSION['openid'])){
    include '../c/config.php';
    $mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
    
    $query=$mysqli->query("select tid from ".$tableVer."_group where openid='".$_SESSION['openid']."' limit 0,1 ");
    $getData=$query->fetch_assoc();
    $tid=$getData['tid'];
    $queryT=$mysqli->query("select name from ".$tableVer."_team where id='".$tid."' limit 0, 1");
    $teamName='';
    if($queryT->num_rows>0){
       $getTeam=$queryT->fetch_assoc();
       $teamName=$getTeam['name'];
    }
    $userInfo=array();
    
    $query=$mysqli->query("select openid from ".$tableVer."_group where tid='".$tid."' limit 0, 3");
    $headImg=array();
    $totalFen=0;
    $renShu=0;
    while ($list=$query->fetch_assoc()){
        $QUser=$mysqli->query("select nickname,headimg from ".$tableVer."_users where openid='".$list['openid']."' limit 0, 1");
        $getUser=$QUser->fetch_assoc();
        $QFen=$mysqli->query("select fen from ".$tableVer."_fen where openid='".$list['openid']."' limit 0, 1");
        $thisFen=0;
        if($QFen->num_rows>0){
          $getFen=$QFen->fetch_assoc();
          $totalFen=$totalFen + $getFen['fen'];
          $thisFen=$getFen['fen'];
        }
        $isSelf='n';
        if ($_SESSION['openid'] == $list['openid']){
            $isSelf='y';
        }
        $renShu++;
        $userInfo[]=array('isf'=>$isSelf,'name'=>base64_decode($getUser['nickname']),'a'=>$getUser['headimg'],'f'=>$thisFen);
        $headImg[]=$getUser['headimg'];
    }
    $mysqli->query("update ".$tableVer."_team set fen=".$totalFen.", renshu='".$renShu."' where id='".$tid."' ");
    
    if($renShu < 3){
        $getPaiM=array('count'=>'');
    }else{
        $queryPaiM=$mysqli->query("select count(id) as count from ".$tableVer."_team  where fen > ".$totalFen."  ");
        $getPaiM=$queryPaiM->fetch_assoc();
    }
    if (empty($teamName)){
        echo json_encode(array('s'=>'n'));
    }else{
        echo json_encode(array('s'=>'y','up'=>$getPaiM['count'],'tn'=>$teamName, 'tou'=>$headImg,'u'=>$userInfo,'fen'=>$totalFen));
    }

    
}
?>
